package com.sikaryofficial.backend.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sikaryofficial.backend.domain.dto.req.material.MaterialLibraryListReq;
import com.sikaryofficial.backend.domain.dto.req.material.MaterialLibraryReq;
import com.sikaryofficial.backend.domain.dto.resp.materical.MaterialLibraryDTO;
import com.sikaryofficial.backend.domain.entity.Material;
import com.sikaryofficial.backend.domain.mapping.MaterialLibraryMapping;
import com.sikaryofficial.backend.service.IMaterialService;
import com.sikaryofficial.common.core.exception.ServiceException;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;

/**
 * @author : qinjinyuan
 * @desc : 素材库
 * @date : 2024/08/07 8:36
 */
@Component
@Slf4j
@SuppressWarnings(value = "unchecked")
public class MaterialLibraryManager {
    @Autowired
    private IMaterialService materialService;

    public MaterialLibraryDTO getInfo(Long materialId) {
        if (Objects.isNull(materialId)) {
            throw new ServiceException("materialId is null");
        }
        Material material = materialService.getOne(new LambdaQueryWrapper<Material>()
                .eq(Material::getMaterialId, materialId)
                .eq(Material::getDeletedVersion, 0L)
        );
        return MaterialLibraryMapping.INSTANCE.coverEntityToDTO(material);
    }

    public Long saveOrUpdate(MaterialLibraryReq req) {
        Material material = MaterialLibraryMapping.INSTANCE.coverReqToEntity(req);
        if (Objects.isNull(req.getMaterialId())) {
            material.setMaterialId(IdWorker.getId());
            material.setCreatedBy(SecurityUtils.getUserId());
            material.setCreatedTime(new Date());
        } else {
            material.setUpdatedBy(SecurityUtils.getUserId());
            material.setUpdatedTime(new Date());
        }
        materialService.saveOrUpdate(material);
        return material.getMaterialId();
    }

    public void remove(String materialIds) {
        materialService.update(new LambdaUpdateWrapper<Material>()
                .in(Material::getMaterialId, materialIds)
                .eq(Material::getDeletedVersion, 0L)
                .set(Material::getUpdatedBy, SecurityUtils.getUserId())
                .set(Material::getUpdatedTime, new Date())
                .setSql(" deleted_version = material_id ")
        );
    }

    public IPage<MaterialLibraryDTO> listPage(MaterialLibraryListReq req) {
        Page<Material> customerPage = new Page<>(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<Material> queryWrapper = buildQueryWrapper(req);
        IPage<Material> prizePage = materialService.page(customerPage, queryWrapper);
        if (CollUtil.isEmpty(prizePage.getRecords())) {
            return new Page<>(1, 7, 0);
        }
        if (CollUtil.isEmpty(prizePage.getRecords())) {
            return new Page<>(req.getPageNum(), req.getPageSize(), 0);
        }
        return prizePage.convert(MaterialLibraryMapping.INSTANCE::coverEntityToDTO);
    }

    private LambdaQueryWrapper<Material> buildQueryWrapper(MaterialLibraryListReq req) {
        return new LambdaQueryWrapper<Material>().eq(Material::getDeletedVersion, 0L)
                .likeRight(CharSequenceUtil.isNotBlank(req.getTitle()), Material::getTitle, req.getTitle())
                .orderByDesc(Material::getCreatedTime, Material::getUpdatedTime, Material::getMaterialId);
    }
}
